package com.study.threadpool.service;

import com.study.threadpool.helper.ThreadPoolHelper;
import com.study.threadpool.task.LoveTask;
import org.springframework.stereotype.Service;
import sun.nio.ch.ThreadPool;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author flwu
 * @date 2020/3/11 5:25 PM
 * @MOTTO 保持空和敬畏
 */

@Service
public class TestThreadPoolService {

    private static final int CORE_POOL_SIZE = 10;
    private static final int MAX_POOL_SIZE = 20;
    private static final int KEEP_ALIVE_TIME = 2;
    private static final int MAX_QUEUE_SIZE = 10;

    private ThreadPoolExecutor taskOneExecutor;
    private ThreadPoolExecutor taskTwoExecutor;
    private ThreadPoolExecutor taskThreeExecutor;


    public void init() {
        taskOneExecutor = ThreadPoolHelper.createThreadPool("taskOne-");
        taskTwoExecutor = ThreadPoolHelper.createThreadPool("taskTwo-");
        taskThreeExecutor = ThreadPoolHelper.createThreadPool("taskThree-", CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, MAX_QUEUE_SIZE);
    }

    public void test2() {
        System.out.println("执行任务操作");
    }

    /**
     * 创建默认线程池，并执行任务
     */
    public void test1() {
        ThreadPoolExecutor executor = ThreadPoolHelper.createThreadPool("test1");
        //正常写法
        try {
            for(int i=0; i < 30; i++) {
                //lambda写法
                executor.execute(() -> {
                    //打印当前线程名字
                    System.out.println(Thread.currentThread().getName());
                    //执行任务
                    test2();
                    //查看线程池情况
                    System.out.println("队列大小：" + executor.getQueue().size());
                    System.out.println("活跃数：" + executor.getActiveCount());
                    System.out.println("执行完成的数量:" + executor.getCompletedTaskCount());
                    System.out.println("任务总数:" + executor.getTaskCount());
                    try {
                        Thread.sleep(2000);
                    } catch (Exception es) {
                        es.printStackTrace();
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Thread.sleep(10000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //由于非守护job，需要手动关闭线程池，也可以不用，主线程结束，线程池及结束
        executor.shutdown();
        System.out.println("执行完成");
    }

    /**
     * 创建自定义线程池，并执行任务
     */
    public void test3() {
        ThreadPoolExecutor executor = ThreadPoolHelper.createThreadPool("flwu-", 10, 20, 1, 10);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                //打印当前线程名字
                System.out.println(Thread.currentThread().getName());
                //执行任务
                test2();
                //查看线程池情况
                System.out.println("队列大小：" + executor.getQueue().size());
                System.out.println("活跃数：" + executor.getActiveCount());
                System.out.println("执行完成的数量:" + executor.getCompletedTaskCount());
                System.out.println("任务总数:" + executor.getTaskCount());
                try {
                    Thread.sleep(2000);
                } catch (Exception es) {
                    es.printStackTrace();
                }
            }
        });
        try {
            Thread.sleep(10000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.print("执行完成");
    }

    /**
     * 执行自定义线程
     */
    public void test4() {
        ThreadPoolExecutor executor = ThreadPoolHelper.createThreadPool("love-love");
        //十对情侣调情记事
        for (int i = 0; i < 10; i++) {
            executor.execute(new LoveTask());
        }
        try {
            Thread.sleep(10000);
        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("在一起了 ....");
    }

}
